//
//  ZSpotLightHelper.m
//  danpin
//
//  Created by chuangjia on 30/12/2015.
//  Copyright © 2015 chuangjia. All rights reserved.
//

#import "ZSpotLightHelper.h"
#import "StorageInfo.h"
#import "NSDictionary+ObjectExt.h"
@import CoreSpotlight;
@import UIKit;

@interface ZSpotLightHelper ()

@property (nonatomic, strong) NSMutableArray *searchItems;

@end

@implementation ZSpotLightHelper

+ (instancetype)helper
{
    static dispatch_once_t onceToken;
    static ZSpotLightHelper *helper;
    dispatch_once(&onceToken, ^{
        helper = [[self alloc] init];
    });
    return helper;
}
- (void)deleteAllSearchAbleItems
{
    [[CSSearchableIndex defaultSearchableIndex] deleteAllSearchableItemsWithCompletionHandler:^(NSError * _Nullable error) {
        if (error) {
            NSLog(@"%@", error.localizedDescription);
        } else {
            NSLog(@"Delete all success");
        }
    }];
}
- (NSMutableArray *)searchItems
{
    if (!_searchItems) {
        _searchItems = [[NSMutableArray alloc] init];
    }
    return _searchItems;
}
- (void)setSpotlightSearch:(NSMutableArray*)data
{
    if (data && data.count>0 && [CSSearchableIndex isIndexingAvailable])
    {
          NSMutableArray *seachableItems = [NSMutableArray array];
          [data enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                if (obj && [obj isKindOfClass:[NSDictionary class]]) {
                    id dataa =[obj objectForKey:@"data"];
                    if (dataa && [dataa isKindOfClass:[NSDictionary class]]) {
                        //1.创建一个set 将信息添加进对应的属性中
                        CSSearchableItemAttributeSet *attributeSet = [[CSSearchableItemAttributeSet alloc] initWithItemContentType:@"string"/*这里要指定信息类型*/];
                        
                        //2.将name的值付给title
                        attributeSet.title = [dataa stringForKey:@"post_title"];
                        attributeSet.path =[dataa stringForKey:@"search_url"];
                        CSSearchableItem *itemType = [[CSSearchableItem alloc] initWithUniqueIdentifier:[NSString stringWithFormat:@"%ld", idx] domainIdentifier:[NSString stringWithFormat:@"com.danpin.coreSpotLight.%ld", idx] attributeSet:attributeSet];
                        itemType.expirationDate = [NSDate dateWithTimeIntervalSinceNow:20];
                        
                        //6. 将SearchableItem对象添加进数组.
                        [seachableItems addObject:itemType];
                        itemType=nil;
                    }
                }
            }];
        if (seachableItems && seachableItems.count>0) {
            [[CSSearchableIndex defaultSearchableIndex] indexSearchableItems:seachableItems completionHandler:^(NSError * __nullable error) {}];
            self.searchItems = [NSMutableArray arrayWithArray:seachableItems];
        }
        
    }
    
}
- (NSString *)getItemData:(NSString*)title
{
    NSString *dict =@"";
    for (id obj in self.searchItems) {
        if (obj && [obj isKindOfClass:[CSSearchableItem class]]) {
            CSSearchableItem *attr = (CSSearchableItem *)obj;
            NSString *tite = attr.attributeSet.title;
            if ([tite isEqualToString:title]) {
                dict = attr.attributeSet.path;
                break;
            }
        }
    }
    return dict;
}
@end